﻿using System;
using System.Linq;
using System.Windows.Forms;
using Relocation.Data;
using System.Data.Objects;
using Relocation.Classes;
using Relocation.Base;
using Relocation.Com;
using Relocation.Forms.ListBase;


namespace Relocation
{
	public partial class projectList : ProjectListBase, IPrintForm
	{
		public projectList()
			: base()
		{
			Initialize();
		}

		public projectList(Session session)
			: base(session)
		{
			Initialize();
		}

		#region 初始化
		private void Initialize()
		{
			InitializeComponent();
			this.ObjectQuery = this.Session.DataModel.Projects;
			this.InitializeRole();
			this.InitializeSearch();
			this.projectDataview.init();
		}

		/// <summary>
		/// 初始化权限
		/// </summary>
		private void InitializeRole()
		{
			ControlTag roleTag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));
			this.ButtonAdd.Tag = roleTag;
			this.ButtonEdit.Tag = roleTag;
			this.ButtonDel.Tag = roleTag;
		}

		/// <summary>
		///  初始化搜索
		/// </summary>
		private void InitializeSearch()
		{
			this.Field_S_Project.Tag = new ControlTag(null, null, new SearchInfo(Projects.GetPropName(t => t.projectName)));
		} 
		#endregion

		//页面刷新
		public override void Refresh()
		{
			try
			{
				this.Reload();
			} catch (Exception ex)
			{
				Log.Error(ex.Message);
			}
		}


		//新增
		private void add_Click(object sender, EventArgs e)
		{
			try
			{
				using (ProjectWindow projectWin = new ProjectWindow(Session))
				{
					projectWin.Text = "项目登记";
					if (projectWin.ShowDialog().IsOK())
						this.Refresh();
				}
			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error("操作失败！");
			}
		}

		//修改
		private void alter_Click(object sender, EventArgs e)
		{
			try
			{
				Projects p = this.GetSelectEntity();
				ProjectWindow projectWin = new ProjectWindow(Session, p);
				projectWin.Text = "项目修改";
				if (projectWin.ShowDialog().IsOK())
				{
					if (p.projectID == Session.Project.projectID)
					{
						MainWindow mainWin = (MainWindow)this.FindForm().Parent.FindForm();
						mainWin.curProject.Text = p.projectName;
					}
					this.Refresh();
				}

			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error("操作失败！");
			}
		}

		//删除
		private void delete_Click(object sender, EventArgs e)
		{
			try
			{

				Projects project = this.GetSelectEntity();
				if (project.projectID == Session.Project.projectID)
				{
					MyMessagebox.Show("当前是系统选择项目不允许删除！");
					return;
				} else if (project.isUsed)
				{
					MyMessagebox.Show("您所要删除的项目下已有拆迁户，不能删除！");
					return;
				} else
				{
					if (MyMessagebox.Confirm("您确定要删除该项目吗？") == DialogResult.Yes)
					{
						string strPath;
						if (project.filePath != null)
							strPath = project.filePath;
						else
							strPath = "";

						if (Session.DataModel.Delete(project) < 1)
						{
							MyMessagebox.Show("删除失败！");
							return;
						} else
						{
							if (string.IsNullOrEmpty(strPath.Trim()))
							{
								if (System.IO.File.Exists(strPath))
									System.IO.File.Delete(strPath);
								else
									MyMessagebox.Show("许可证文件不存在，有可能被删除或转移！");
							}
						}
					}

				}
				this.Refresh();
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}

		#region IPrintForm 成员

		void IPrintForm.Print()
		{
			GridToExcel.print(this.projectDataview);
		}

		#endregion
	}
}
